/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.util;


/**
 * Generic "variables setter" 
 * 
 * @author Laurent GUERIN
 *  
 */
public abstract class VarSetter //extends TelosysObject
{    
    /**
     * Returns the value of the given variable name  
     * @param sVarName
     * @return the variable value
     */
    protected abstract String getVarValue( String sVarName );
    
    /**
     * Recursive method for variables replacement 
     * @param s
     * @param iCount
     * @return the string after processing 
     */
    private String replaceVar( String s, int iCount)
    {
        if ( iCount > 100 ) // Recursion is limited to 100 calls 
        {
            return s; // Max. calls exceeded => return the string "as is"
        }
        
        String sNewString = s ;
        int i = s.indexOf('@'); // Search the first "@"
        if ( i >= 0 ) 
        {
            if ( s.charAt(i+1) == '{')
            {
                int j = s.indexOf('}');
                if ( j > i+1 )
                {
                    String s1 = s.substring(0,i);
                    String s2 = s.substring(j+1,s.length());
                    String sVar = s.substring(i+2, j);
                    //--- Replace the var name by the var value
                    String sVarValue = getVarValue(sVar) ;
                    sNewString = s1 + sVarValue + replaceVar( s2, iCount+1 ) ;
                }
            }   
            else
            {
                int j = s.indexOf('}');
                if ( j > i+1 )
                {
                    String s1 = s.substring(0,j+1);
                    String s2 = s.substring(j+1,s.length());
                    sNewString = s1 + replaceVar( s2, iCount+1 );
                }
            }
        }
        return sNewString ;
    }

    /**
     * Replace all the variables "@{var}" in the given string by their value<br>
     * using the "getVarValue" method.
     * 
     * @param s the string containing the variable names : "@{.attrib1} blabla @{.attrib2}" or "@{1} blabla @{2}" 
     * @return the string after variables replacement ( or null if the given string is null )
     */
    public String replaceVar( String s )
    {
        if ( s != null )
        {
            return replaceVar( s, 1 );            
        }
        return s ;
    }
}